package com.ohc.leetcode.二叉树;

import com.ohc.leetcode.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 2022年5月2日14:20:31
 */
public class 二叉树的最大深度104 {

    public static int maxDepth1(TreeNode root) {
        if (root == null) {
            return 0;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        int maxL = 0;
        while (!queue.isEmpty()) {

            int len = queue.size();
            while (len > 0) {

                TreeNode node = queue.poll();
                if (node.left != null) {
                    queue.offer(node.left);
                }
                if (node.right != null) {
                    queue.offer(node.right);
                }

                len--;
            }
            maxL++;
        }
        return maxL;
    }

    /**
     * 递归方法求最大深度
     *
     * @param root
     * @return
     */
//    public static int maxDepth2(TreeNode root) {
//        if (root == null) {
//            return 0;
//        }
//
//    }

    public static void main(String[] args) {
        TreeNode treeNode1 = new TreeNode();
        TreeNode treeNode2 = new TreeNode();
        TreeNode treeNode3 = new TreeNode();
        treeNode1.right = treeNode2;
        treeNode1.left = treeNode3;
        treeNode2.right = null;
        treeNode2.left = null;
        treeNode3.right = null;
        treeNode3.left = null;
        treeNode1.val = 1;
        treeNode2.val = 2;
        treeNode3.val = 3;
        int max = maxDepth1(treeNode1);
        System.out.println("max = " + max);
    }
}
